// -------------------------------------------------------------
// Copyright(C) General Software, Inc., Minnesota, USA
// This block of code are developed independently by
// General Software, Inc., Minnesota. You may use it, but
// you may not modify it and sell it to anyone in the world.
// -------------------------------------------------------------
using System;
using System.Data;
using System.Collections;

namespace PNet.Icap.PluginSDK.SQLCeAccess
{
	#region CommandExeType, ParamType, ParamRequired
    /// <summary>
    /// command type
    /// </summary>
	public enum CommandExeType
	{
        /// <summary>
        /// query proc
        /// </summary>
		QueryProc,
        /// <summary>
        /// non query proc
        /// </summary>
		NonQueryProc,
        /// <summary>
        /// sql command text
        /// </summary>
		SqlCeCommandText,
        /// <summary>
        /// table direct
        /// </summary>
		TableDirect
	}

    /// <summary>
    /// param type
    /// </summary>
	public enum ParamType
	{
        /// <summary>
        /// input
        /// </summary>
		Input,
        /// <summary>
        /// output
        /// </summary>
		Output,
        /// <summary>
        /// input / output
        /// </summary>
		InputOutput,
        /// <summary>
        /// return value
        /// </summary>
		ReturnValue
	};

	/// <summary>
	/// If parameter is assigned to null in the proc parameter list, then
	/// this parameter is not required.
	/// </summary>
	public enum ParamRequired
	{
        /// <summary>
        /// yes
        /// </summary>
		Yes,
        /// <summary>
        /// no
        /// </summary>
		No
	};
	#endregion

	#region type_command_info
	/// <summary>
	/// Use this type to label a sql command class to specify command information
	/// </summary>
	[AttributeUsage(AttributeTargets.Class)] public class type_command_info : Attribute
	{
        /// <summary>
        /// def db timeout
        /// </summary>
		public const int _DefDBTimeout = 90;
		private string _commandText = ""; 
		private CommandType _commandType;
		private CommandExeType _cmdExeType;
		private int _commandTimeout;

		/// <summary>
		/// sql proc or command information
		/// </summary>
		/// <param name="commandText"></param>
		/// <param name="exeType"></param>
		/// <param name="timeOut"></param>
		public type_command_info(string commandText, CommandExeType exeType, int timeOut)
		{
			this._commandText = commandText;
			this._cmdExeType = exeType;
			this._commandType = GetCommandType (exeType);
			this._commandTimeout = timeOut;
		}

		/// <summary>
		/// sql proc or command information
		/// </summary>
		/// <param name="commandText"></param>
		/// <param name="exeType"></param>
		public type_command_info(string commandText, CommandExeType exeType)
		{
			this._commandText = commandText;
			this._cmdExeType = exeType;
			this._commandType = GetCommandType (exeType);
			this._commandTimeout = _DefDBTimeout;
		}
        /// <summary>
        /// type command info
        /// </summary>
		public type_command_info()
		{
		}
		/// <summary>
		/// command text
		/// </summary>
		public string CommandText {get {return this._commandText;}}
        /// <summary>
        /// command type
        /// </summary>
		public CommandType CommandType {get {return this._commandType;}}
        /// <summary>
        /// command exe type
        /// </summary>
		public CommandExeType CommandExeType {get {return this._cmdExeType;}}
        /// <summary>
        /// command timeout
        /// </summary>
		public int CommandTimeout {get {return this._commandTimeout; }}

		private CommandType GetCommandType (CommandExeType exeType)
		{
			switch (exeType)
			{
				case CommandExeType.NonQueryProc:
					return CommandType.StoredProcedure;
				case CommandExeType.QueryProc:
					return CommandType.StoredProcedure;
				case CommandExeType.SqlCeCommandText:
					return CommandType.Text;
				case CommandExeType.TableDirect:
					return CommandType.TableDirect;
			}
			return CommandType.StoredProcedure;
		}
	}
	#endregion

	#region type_param_info
	/// <summary>
	/// Stored procedure parameter information
	/// </summary>
	[AttributeUsage(AttributeTargets.All)] public class type_param_info : Attribute 
	{
		private int _size;
		private string _name;
		private ParamRequired _ParamRequired;
		private ParamType _paramType;

		/// <summary>
		/// Stored proc parameter information
		/// </summary>
		/// <param name="paramType">input, output, inputoutput or return value</param>
		/// <param name="name">actural parameter name including @ char</param>
		/// <param name="paramRequired">Indicates if the param is a null field assgined in the proc. If null assigned, the it is not a required field.</param>
		/// <param name="size">string length if it is a char, varchar, nvarchar, or nchar parameter</param>
		public type_param_info (ParamType paramType, string name, ParamRequired paramRequired, int size)
		{
			_size = size;
			_name = name;
			_ParamRequired = paramRequired;
			_paramType = paramType;
		}
		/// <summary>
		/// Stored proc parameter information
		/// </summary>
		/// <param name="paramType">input, output, inputoutput or return value</param>
		/// <param name="name">actural parameter name including @ char</param>
		/// <param name="paramRequired">Indicates if the param is a null field assgined in the proc. If null assigned, the it is not a required field.</param>
		public type_param_info (ParamType paramType, string name, ParamRequired paramRequired)
		{
			_name = name;
			_paramType = paramType;
			_size = 0;
			_ParamRequired = paramRequired;
		}
		/// <summary>
		/// Stored proc parameter information
		/// </summary>
		/// <param name="paramType">input, output, inputoutput or return value</param>
		/// <param name="name">actural parameter name including @ char</param>
		/// <param name="size">string length if it is a char, varchar, nvarchar, or nchar parameter</param>
		public type_param_info (ParamType paramType, string name, int size)
		{
			_name = name;
			_paramType = paramType;
			_size = size;
			_ParamRequired = ParamRequired.Yes;
		}
		/// <summary>
		/// Stored proc parameter information
		/// </summary>
		/// <param name="paramType">input, output, inputoutput or return value</param>
		/// <param name="name">actural parameter name including @ char</param>
		public type_param_info (ParamType paramType, string name)
		{
			_name = name;
			_paramType = paramType;
			_size = 0;
			_ParamRequired = ParamRequired.Yes;
		}
        /// <summary>
        /// size
        /// </summary>
		public int Size {get{return _size;}}
        /// <summary>
        /// name
        /// </summary>
		public string Name {get{return _name;}}
        /// <summary>
        /// param type
        /// </summary>
		public ParamType ParamType {get {return _paramType;}}
        /// <summary>
        /// param required
        /// </summary>
		public ParamRequired ParamRequired {get{return _ParamRequired;}}
	}
	#endregion

	/// <summary>
	/// Base class for SqlHelperSQLDB and SqlHelperOleDB
	/// </summary>
	public class SqlCeHelperBase
	{
        /// <summary>
        /// Return value
        /// </summary>
		public object returnValue = null;
        /// <summary>
        /// Connetion string
        /// </summary>
		protected string _DBConnString = "";
        /// <summary>
        /// Command info
        /// </summary>
		protected type_command_info _cmdInfo;
        /// <summary>
        /// Assigned fields
        /// </summary>
		protected Hashtable _assignedFields;

        /// <summary>
        /// Constructor
        /// </summary>
		public SqlCeHelperBase ()
		{
			 _cmdInfo = GetCommandInfo ();
		}
        /// <summary>
        /// Command info
        /// </summary>
		protected type_command_info commandInfo
		{
			get { return _cmdInfo; }
		}
        /// <summary>
        /// Get field name
        /// </summary>
        /// <param name="attrField"></param>
        /// <returns></returns>
		protected string GetFieldName (object attrField)
		{
			return (string)ReflectionHelper.GetFieldValue(attrField, "Name");
		}
        /// <summary>
        /// Get param type
        /// </summary>
        /// <param name="attrField"></param>
        /// <returns></returns>
		protected ParamType GetParamType(object attrField)
		{
			return (ParamType)ReflectionHelper.GetFieldValue(attrField, "ParamType");
		}
        /// <summary>
        /// Get field size
        /// </summary>
        /// <param name="attrField"></param>
        /// <returns></returns>
		protected int GetFieldSize (object attrField)
		{
			return (int)ReflectionHelper.GetFieldValue(attrField, "Size");
		}
        /// <summary>
        /// Get param required
        /// </summary>
        /// <param name="attrField"></param>
        /// <returns></returns>
		protected ParamRequired GetParamRequired(object attrField)
		{
			return (ParamRequired)ReflectionHelper.GetFieldValue(attrField, "ParamRequired");
		}
        /// <summary>
        /// Sql data time valid
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
		protected bool SqlDateTimeValid (DateTime dt)
		{
			DateTime dt0 = DateTime.Parse ("1/1/1753 12:00:00 AM");
			DateTime dt1 = DateTime.Parse ("12/31/9999 11:59:59 PM");
			if(dt < dt0 || dt > dt1)
				return false;
			else
				return true;
		}
        /// <summary>
        /// Add param
        /// </summary>
        /// <param name="paramName"></param>
		protected void addparam (string paramName)
		{
			if(_assignedFields == null) _assignedFields = new Hashtable ();
			_assignedFields.Add(paramName, paramName);
		}
        /// <summary>
        /// Clear param
        /// </summary>
		protected void clearparam ()
		{
            if (_assignedFields != null)
                _assignedFields.Clear();
		}
        /// <summary>
        /// To command exe type
        /// </summary>
        /// <param name="exeType"></param>
        /// <returns></returns>
		public static CommandExeType ToCommandExeType (string exeType)
		{
			switch (exeType)
			{
				case "Query":
					return CommandExeType.QueryProc;
				case "NonQueryProc":
					return CommandExeType.NonQueryProc;
				case "SqlCeCommandText":
					return CommandExeType.SqlCeCommandText;
				case "TableDirect":
					return CommandExeType.TableDirect;
			}
			return CommandExeType.QueryProc;
		}

		#region GetCommandInfo
		/// <summary>
		/// Return command_info
		/// </summary>
		/// <returns></returns>
		private type_command_info GetCommandInfo ()
		{
			object[] memberInfo = this.GetType ().GetCustomAttributes (true);
			foreach(object m in memberInfo)
			{
				if(m.GetType () == typeof(PNet.Icap.PluginSDK.SQLCeAccess.type_command_info))
				{
					type_command_info cmd = (type_command_info)m;
					return cmd;
				}
			}
			return null;
		}
		#endregion
        /// <summary>
        /// to where clause string
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
		protected string toWhereClauseStr (object val)
		{
			switch(val.GetType().Name)
			{
				case "String":
					return string.Format("'{0}'", (string)val);
				case "Byte":
					return string.Format("{0}", val);
				case "Char":
					return string.Format("'{0}'", (string)val);
				case "Guid":
					return string.Format("'{0}'", val.ToString());
				case "DateTime": 
					return string.Format("#{0}#", val.ToString());
				case "Boolean":
                    return string.Format("{0}", val);
				case "DBNull":
					return "NULL";
				case "Empty": 
					return "NULL";
				case "Decimal": 
				case "Double":
				case "Int16":
				case "Int32": 
				case "Int64":
				case "SByte": 
				case "Single":
				case "UInt16":
				case "UInt32": 
				case "UInt64":
					return string.Format("{0}", val);
			}
			return string.Format("{0}", val);
		}
	}
}
// -------------------------------------------------------------
// Copyright(C) General Software, Inc., Minnesota, USA
// This block of code are developed independently by
// General Software, Inc., Minnesota. You may use it, but
// you may not modify it and sell it to anyone in the world.
// -------------------------------------------------------------
